//
//  XMSearchViewController.m
//  XunMi
//
//  Created by MacBook on 2024/4/28.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMSearchViewController.h"
#import "XMSearchNavView.h"
#import "XMSearchTypeView.h"
#import "XMHomeListTableViewCell.h"
#import "XMUserDetailViewController.h"
#import "XMConversationViewController.h"

@interface XMSearchViewController ()
@property (nonatomic, strong) XMSearchNavView *searchView;
@property (nonatomic, strong) XMSearchTypeView *searchTypeView;
@property (nonatomic, assign) XMSearchType searchType;

@property (nonatomic, strong) NSMutableArray <XMUserModel *> *dataArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation XMSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self viewBlock];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.searchType = XMSearchByName;
    self.dataArray = [NSMutableArray array];
    self.page = 1;
    [self tableViewRegisterNibWithNibName:kStringFromClass(XMHomeListTableViewCell)];
}

- (void)searchListData {
    [MBProgressHUD showHUDActivityMsg:nil];
    [self.dataArray removeAllObjects];
    NSMutableDictionary *dic = @{
        @"page_no": @(self.page),
        @"page_size": @(10),
        @"search_type": @(self.searchType),
        @"search_value": self.searchView.textField.text
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/index/search" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMUserModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                self.tableView.statusTitleLabel.text = @"暂时未搜索到哦~";
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - setupSubViews
- (void)setupSubviews {
    self.searchView = [XMSearchNavView viewFromXib];
    self.searchView.type = self.searchType;
    [self.navView addSubview:self.searchView];
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.navLeftBtn.mas_right).offset(0);
        make.centerY.equalTo(self.navLeftBtn);
        make.height.mas_equalTo(38);
        make.right.equalTo(self.navView).offset(-18);
    }];
    
    self.searchTypeView = [XMSearchTypeView viewFromXib];
    self.searchTypeView.hidden = YES;
    [self.view addSubview:self.searchTypeView];
    [self.searchTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchView);
        make.top.equalTo(self.navView.mas_bottom);
        make.size.mas_equalTo(CGSizeMake(64, 77));
    }];
}

#pragma mark - viewBlock
- (void)viewBlock {
    XMWeakSelf(self);
    [self.searchView setupChangeBlock:^{
        XMStrongSelf(self);
        self.searchTypeView.hidden = NO;
    } searchBlock:^(XMSearchType type, NSString * _Nonnull text) {
        XMStrongSelf(self);
        if ([XMUtil isBlankString:text]) {
            [MBProgressHUD showHUDMsg:self.searchView.textField.placeholder toView:nil];
            return;
        }
        [self.searchView.textField resignFirstResponder];
        [self searchListData];
    } clearBlock:^{
        XMStrongSelf(self);
        [self.dataArray removeAllObjects];
        [self.tableView reloadData];
        [self.tableView hideStatusView];
    }];
    
    self.searchTypeView.changeBlock = ^(NSInteger type) {
        XMStrongSelf(self);
        self.searchType = type;
        self.searchView.type = type;
        self.searchTypeView.hidden = YES;
    };
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMHomeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kStringFromClass(XMHomeListTableViewCell) forIndexPath:indexPath];
    XMUserModel *model = [self.dataArray safe_objectAtIndex:indexPath.row];
    cell.model = model;
    XMWeakSelf(self);
    cell.chatBlock = ^{
        XMStrongSelf(self);
        XMConversationViewController *conversationVC = [[XMConversationViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:model.user_id];
        conversationVC.conversationType = ConversationType_PRIVATE;
        conversationVC.targetId = model.user_id;
        conversationVC.titleStr = model.showName;
        conversationVC.unReadMessage = 0;
        conversationVC.enableNewComingMessageIcon = YES; //开启消息提醒
        conversationVC.enableUnreadMessageIcon = YES;
        [self.navigationController pushViewController:conversationVC animated:YES];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 84;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XMUserModel *model = [self.dataArray safe_objectAtIndex:indexPath.row];
    XMUserDetailViewController *vc = [[XMUserDetailViewController alloc] init];
    vc.userId = model.user_id;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
